<template>
	<view>
		<view class="cu-form-group align-start">
			<view class="title">ApiKey</view>
			<textarea maxlength="-1" v-model="formData.api_key" placeholder="交易所账户API密钥"></textarea>
		</view>
		<view class="cu-form-group align-start">
			<view class="title">SecretKey</view>
			<textarea maxlength="-1" v-model="formData.secret_key" placeholder="交易所账户Secret密钥"></textarea>
		</view>
		<view v-if="basic.exchange_domain === formData.exchange_domain" class="cu-form-group align-start">
			<view class="title">Passphrase</view>
			<input class="title" v-model="formData.passphrase" placeholder="交易所账户API密码"></input>
		</view>
		<view class="cu-form-group align-start">
			<view class="title">服务地址</view>
			<input class="title" v-model="formData.ip_address" placeholder="后端机器人程序接口IP地址" @blur="isBlur"></input>
		</view>
		<view class="cu-form-group align-start">
			<view class="title">接口账户</view>
			<input class="title" v-model="formData.username" placeholder="后端机器人程序接口账户" style="margin-top: 14px;" @blur="isBlur"></input>
		</view>
		<view class="cu-form-group align-start">
			<view class="title">接口密码</view>
			<input class="title" type="password" v-model="formData.password" placeholder="后端机器人程序接口密码" style="margin-top: 14px;" @blur="isBlur"></input>
			<button class='cu-btn round' :class="'bg-'+basic.color" style="margin-top: 6px;" @click="verification">验证</button>
		</view>
		<view class="padding-xl">
			<button class="cu-btn block lg" :class="'line-'+basic.color" @click="reset">重 置</button>
			<button class="cu-btn block margin-tb-sm lg" :class="'bg-'+basic.color" @click="submit">提 交</button>
		</view>
		<view class="flex-sub text-center">
			<view class="padding">此数据上传到机器人接口程序中</view>
		</view>
	</view>
</template>

<script>
	import { login, updateRobotApiKey } from '@/api/basic/basic.js'
	export default {
		props: {
			basic: {}
		},
		data() {
			return {
				VerificationData: {
					verification: false,
					ip_address: '',
					username: '',
					password: ''
				},
				formData: {
					ip_address: '',
					api_key: '',
					secret_key: '',
					passphrase: '',
					username: '',
					password: '',
					exchange_domain: 'okx',
					access_token: '',
					refresh_token: ''
				}
			}
		},
		mounted() {
			console.log('配置', this.basic)
		},
		methods: {
			// 确认是否需要重新点击验证
			isBlur() {
				if (this.formData.ip_address !== this.VerificationData.ip_address || this.formData.username !== this.VerificationData.username || this.formData.password !== this.VerificationData.password) {
					this.VerificationData.verification = false
				}
			},
			// 验证登录
			verification() {
				console.log('点击了登录验证')
				if (this.formData.ip_address && this.formData.username && this.formData.password) {
					uni.showLoading({ title: '正在验证...' })
					
					login(this.formData.ip_address, {
						'username':this.formData.username, 'password':this.formData.password
					}).then(res =>{
						console.log('登录回调', res)
						if (res.data.access) {
							console.log('access', res.data.access)
							console.log('refresh', res.data.refresh)
							this.formData.access_token = res.data.access
							this.formData.refresh_token = res.data.refresh
							// 点击验证后复制一份用于确认是否已点击
							this.VerificationData = {
								verification: true,
								ip_address: this.formData.ip_address,
								username: this.formData.username,
								password: this.formData.password
							}
							uni.showLoading({ title: '验证成功' })
							res.data.ip_address = this.formData.ip_address
							uni.setStorage({
								key:this.basic.exchange_domain + this.basic.strategy_name,
								data:res.data,
								fail() {
									uni.showLoading({ title: 'token缓存失败' })
								}
							})
						} else {
							uni.showLoading({ title: '验证失败' })
						}
					})
				
					setTimeout(function () {
						uni.hideLoading();
					}, 2000);
				} else {
					uni.showToast({ icon: 'none', title: '接口服务地址、账户、密码必填' })
				}
			},
			// 重置表单
			reset() {
				console.log('重置了配置')
				this.formData = {
					ip_address: '',
					api_key: '',
					secret_key: '',
					passphrase: '',
					username: '',
					password: '',
					exchange_domain: 'okx'
				}
			},
			// 检查表单是否有空项
			checkSubmit() {
				console.log('检查提交的数据:', this.formData)
				console.log('页面基本参数:', this.basic)
				
				if (this.formData.ip_address && this.formData.api_key && this.formData.secret_key) {
					
					if (this.basic.exchange_domain === this.formData.exchange_domain) {
						if (this.formData.passphrase) {
							return true
						} else {
							return false
						}
					} else {
						return true
					}
					
				} else {
					return false
				}
				
			},
			// 提交
			submit() {
				console.log('点击了提交配置')
				
				let _this = this
				uni.showLoading({ title: '正在提交...' })
				
				if (this.VerificationData.verification) {
					if (this.checkSubmit()) {
						
						uni.getStorage({
							key:'openId',
							success(openIdRes) {
								// 保存到云数据库
								uniCloud.callFunction({
									name:'config',
									data:{
										open_id:openIdRes.data,
										exchange_domain:_this.basic.exchange_domain,
										strategy_name:_this.basic.strategy_name,
										ip_address:_this.formData.ip_address,
										username:_this.formData.username,
										password:_this.formData.password
									},
									success(callRes) {
										// 保存到后端程序数据库
										console.log(_this.formData.ip_address)
										console.log(_this.formData.access_token)
										console.log(_this.basic.exchange_domain)
										console.log(_this.basic.strategy_name)
										console.log(_this.formData.api_key)
										console.log(_this.formData.secret_key)
										console.log(_this.formData.passphrase)
										
										updateRobotApiKey(_this.formData.ip_address, _this.formData.access_token, {
											'exchange':_this.basic.exchange_domain, 
											'strategy':_this.basic.strategy_name, 
											'api_key':_this.formData.api_key, 
											'secret_key':_this.formData.secret_key, 
											'passphrase':_this.formData.passphrase
										}).then(res => {
											uni.showToast({ icon: 'none', title: '提交成功' })
											setTimeout(function () {
												_this.$emit("barBasic", {
													pages:1, 
													show:true,
													ipAddress:_this.formData.ip_address,
													exchangeDomain:_this.basic.exchange_domain,
													strategy:_this.basic.strategy_name,
												})
											}, 1000);
										}).catch(err => {
											console.log(`保存到机器人失败: ${err.errMsg}`)
											uni.showToast({ icon: 'none', title: `保存到机器人失败: ${err.errMsg}` })
										})
										
									},
									fail(callErr) {
										console.log(`保存配置到云数据库失败: ${callErr.errMsg}`)
										uni.showToast({ icon: 'none', title: `保存配置到云数据库失败: ${callErr.errMsg}` })
									}
								})
								
							},
							fail(openIdErr) {
								console.log(`提交配置数据前获取openId缓存失败: ${openIdErr.errMsg}`)
								uni.showToast({ icon: 'none', title: `提交配置数据前获取openId缓存失败: ${openIdErr.errMsg}` })
							}
						})
						
					} else {
						uni.showToast({ icon: 'none', title: '所有输入框必填' })
					}
				} else {
					uni.showToast({ icon: 'none', title: '需验证' })
				}
				
				setTimeout(function () {
					uni.hideLoading();
				}, 2000);
			}
		}
	}
</script>

<style>

</style>
